package cn.tedu.charging.order.mqtt;

import cn.tedu.chaging.common.utils.JsonUtils;
import cn.tedu.charging.order.pojo.dto.ChargingDto;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MqttProducer {

    @Autowired
    private MqttClient mqttClient;

    private String topic = "topic/start/666";

    /**
     * 给MQTT消息中间件发送消息
     * @param message
     */
    public void send(ChargingDto message){
        try {
            log.debug("MqttProducer-入参:{}",message);
            String json = JsonUtils.toJson(message);
            //通过调用mqttClient发送消息
            byte[] bytes = json.getBytes();
            log.debug("publish-入参:topic-{},message-{}",topic,bytes);
            mqttClient.publish(topic,bytes,0,false);
            log.debug("publish-入参:topic-{},message-{},成功",topic,bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
